package mesweb.mesweb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farvision.common.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mesweb.mesweb.Utils.QueryWrapperUtil;
import mesweb.mesweb.entity.MesTechnologySetFlow;
import mesweb.mesweb.service.IMesTechnologySetFlowService;
import mesweb.mesweb.vo.MesTechnologySetFlowVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 工艺组工作流程关系
 * @Author: jeecg-boot
 * @Date:   2021-06-21
 * @Version: V1.0
 */

@Api(tags="工艺组工作流程关系")
@RestController
@RequestMapping("/mes/mesTechnologySetFlow")
public class MesTechnologySetFlowController {
	@Autowired
	private IMesTechnologySetFlowService mesTechnologySetFlowService;


    @GetMapping("/GetAll")
    @ApiOperation("获取全部数据列表")
    public CommonResult<?> GetAll(@ApiParam("名称") @RequestParam( value = "name",required = false) String name) {
        List<MesTechnologySetFlowVo> data = new ArrayList<>();
        QueryWrapper<MesTechnologySetFlowVo> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isBlank(name) || name.equals("全部")) {
            data = mesTechnologySetFlowService.getTechnologySetFlow(queryWrapper);
        } else {
            queryWrapper.eq("technology_flow_id",name);
            data = mesTechnologySetFlowService.getTechnologySetFlow(queryWrapper);
        }
        return CommonResult.success(data);
    }
	/**
	 * 分页列表查询
	 *
	 * @param mesTechnologySetFlow
	 * @param page
	 * @param size
	 * @param req
	 * @return
	 */
	@ApiOperation(value="工艺组工作流程关系-分页列表查询", notes="工艺组工作流程关系-分页列表查询")
	@GetMapping(value = "/list")
	public CommonResult<?> queryPageList(MesTechnologySetFlow mesTechnologySetFlow,
								   @RequestParam(name="page", defaultValue="1") Integer page,
								   @RequestParam(name="size", defaultValue="10") Integer size,
								   HttpServletRequest req) {
		QueryWrapperUtil<MesTechnologySetFlow> queryWrapperUtil = new QueryWrapperUtil<>();
		QueryWrapper<MesTechnologySetFlow> queryWrapper = queryWrapperUtil.build(MesTechnologySetFlow.class,mesTechnologySetFlow);
		Page<MesTechnologySetFlowVo> pageMesTechnologySetFlowVo = new Page<MesTechnologySetFlowVo>(page, size);
		IPage<MesTechnologySetFlowVo> pageList = mesTechnologySetFlowService.getTechnologySetFlow(pageMesTechnologySetFlowVo, queryWrapper);
		return CommonResult.success(pageList);
	}
	/**
	 * 编辑
	 *
	 * @param mesTechnologySetFlow
	 * @return
	 */
	@ApiOperation(value="工艺组工作流程关系-编辑", notes="工艺组工作流程关系-编辑")
	@PostMapping(value = "/save")
	public CommonResult<?> edit(@RequestBody MesTechnologySetFlow mesTechnologySetFlow) {
		boolean result = false;
        if(mesTechnologySetFlow.getId() == 0L) {
             result = mesTechnologySetFlowService.save(mesTechnologySetFlow);
        }
        else
        {
            result = mesTechnologySetFlowService.updateById(mesTechnologySetFlow);
        }
        return CommonResult.success(result);
	}
	/**
	 * 编辑
	 *
	 * @param mesTechnologySetFlows
	 * @return
	 */
	@ApiOperation(value="工艺组工作流程关系-批量增加", notes="工艺组工作流程关系-批量增加")
	@PostMapping(value = "/saveBatch")
	public CommonResult<?> edit(@RequestBody Collection<MesTechnologySetFlow> mesTechnologySetFlows) {
		boolean result = false;
		try {
			if (mesTechnologySetFlows.size() > 0L) {
				for (MesTechnologySetFlow mesTechnologySetFlow :
						mesTechnologySetFlows) {
					result = mesTechnologySetFlowService.save(mesTechnologySetFlow);
				}
			}
			return CommonResult.success(mesTechnologySetFlows);
		}catch (Exception e)
		{
			return  CommonResult.failed(e.getMessage());
		}
	}
	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="工艺组工作流程关系-通过id删除", notes="工艺组工作流程关系-通过id删除")
	@DeleteMapping(value = "/delete")
	public CommonResult<?> delete(@RequestParam(name="id",required=true) String id) {
		mesTechnologySetFlowService.removeById(id);
		return CommonResult.success("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@ApiOperation(value="工艺组工作流程关系-批量删除", notes="工艺组工作流程关系-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public CommonResult<?> deleteBatch(@ApiParam("MesTechnologySetFlowIDs") @RequestBody List<Long> ids) {
		this.mesTechnologySetFlowService.removeByIds(ids);
		return CommonResult.success("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="工艺组工作流程关系-通过id查询", notes="工艺组工作流程关系-通过id查询")
	@GetMapping(value = "/queryById")
	public CommonResult<?> queryById(@RequestParam(name="id",required=true) String id) {
		MesTechnologySetFlow mesTechnologySetFlow = mesTechnologySetFlowService.getById(id);
		return CommonResult.success(mesTechnologySetFlow);
	}
}
